﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Nate_sRoguelikeGameXNA 
{
    class KeyStroke :IEquatable<KeyStroke>
    {
        public Keys Key {get;set;}
        public bool IsShiftPressed;
        public bool IsAltPressed;
        public bool IsCtrlPressed;
        public double LastKeyPressTime { get; set; }

        public KeyStroke(Keys[] keys, GameTime gameTime)
        {
            LastKeyPressTime =  gameTime.TotalGameTime.TotalMilliseconds;
            IsShiftPressed = keys.Any(k => k == Keys.RightShift || k == Keys.LeftShift);
            IsAltPressed = keys.Any(k => k == Keys.RightAlt || k == Keys.LeftAlt);
            IsCtrlPressed = keys.Any(k => k == Keys.RightControl || k == Keys.LeftControl);
            Key =
                keys.Except(new[]
                                {
                                    Keys.LeftAlt, Keys.LeftControl, Keys.LeftShift, 
                                    Keys.RightAlt, Keys.RightControl, Keys.RightShift
                                }).FirstOrDefault();
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(KeyStroke other)
        {
            return other != null && other.Key == Key && other.IsAltPressed == IsAltPressed && other.IsCtrlPressed == IsCtrlPressed &&
                   other.IsShiftPressed == IsShiftPressed;
        }

        public bool IsNewPress(GameTime currentGameTime)
        {
            return currentGameTime.TotalGameTime.TotalMilliseconds == LastKeyPressTime;
        }
        /// <summary>
        /// Limits the keystroke repeats to once every 250 ms.
        /// </summary>
        /// <param name="currentGameTime"></param>
        /// <returns></returns>
        public bool IsRepeatAllowed(GameTime currentGameTime)
        {
            if (currentGameTime.TotalGameTime.TotalMilliseconds - LastKeyPressTime > 250)
            {
                LastKeyPressTime = currentGameTime.TotalGameTime.TotalMilliseconds;
                return true;
            }
            return false;
        }
        
    }
}
